//
//  UIColor+YFExtesion.m
//  project
//
//  Created by Winny Zhang on 2019/3/23.
//  Copyright © 2019 健健. All rights reserved.
//

#import "UIColor+YFExtesion.h"

CGFloat yf_colorCompoentFrom(NSString *string, NSUInteger start, NSUInteger length) {
    
    NSString *substring = [string substringWithRange:NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat:@"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString:fullHex] scanHexInt:&hexComponent];
    return hexComponent / 255.f;
}

@implementation UIColor (YFExtesion)

/**
 高性能颜色
 
 @param r red
 @param g green
 @param b blue
 @return Color
 */
+ (UIColor *)withRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b {
    return [self withRed:r green:g blue:b alpha:1];
}

/**
 高性能颜色， Red、Green、Blue值相同
 
 @param value Red、Green、Blue值
 @return Color
 */
+ (UIColor *)withRGB:(UInt32)value {
    return [self withRed:value green:value blue:value];
}

/**
 高性能颜色
 
 @param r red
 @param g green
 @param b blue
 @param a alpha
 @return Color
 */
+ (UIColor *)withRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a {
    if (@available(iOS 10.0, *)) {
        return [UIColor colorWithDisplayP3Red:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a];
    } else {
        return [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a];
    }
}

/**
 高性能颜色， Red、Green、Blue值相同
 
 @param value Red、Green、Blue值
 @param a 透明度
 @return Color
 */
+ (UIColor *)withRGB:(UInt32)value alpha:(CGFloat)a {
    return [self withRed:value green:value blue:value alpha:a];
}

/**
 十六进制 转换 UIColor
 
 @param hex 十六进制数字
 @return Color
 */
+ (UIColor *)withHex:(UInt32)hex {
    return [UIColor withHex:hex alpha:1];
}

/**
 十六进制 转换 UIColor
 
 @param hex 十六进制数字
 @param a   透明度
 @return Color
 */
+ (UIColor *)withHex:(UInt32)hex alpha:(CGFloat)a {
    UInt8 r = (hex & 0xff0000) >> 16;
    UInt8 g = (hex & 0x00ff00) >> 8;
    UInt8 b = hex & 0x0000ff;
    return [UIColor withRed:r green:g blue:b alpha:a];
}


/**
 十六进制字符串 转换 UIColor
 
 @param hexString 十六进制字符串
 @return Color
 */
+ (UIColor *)withHexString:(NSString *)hexString {
    CGFloat alpha, red, blue, green;
    // 去掉空格
    NSString *colorString =
    [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([colorString hasPrefix:@"0X"]) colorString = [colorString stringByReplacingOccurrencesOfString:@"0X" withString:@""];
    if ([colorString hasPrefix:@"#"]) colorString = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    
    switch ([colorString length]) {
        case 3: // "#RGB" || "0XRGB"
            alpha = 1.f;
            red = yf_colorCompoentFrom(colorString, 0, 1);
            green = yf_colorCompoentFrom(colorString, 1, 1);
            blue =  yf_colorCompoentFrom(colorString, 2, 1);
            break;
        case 4: // "#ARGB" || "0XARGB"
            alpha = yf_colorCompoentFrom(colorString, 0, 1);
            red = yf_colorCompoentFrom(colorString, 1, 1);
            green = yf_colorCompoentFrom(colorString, 2, 1);
            blue =  yf_colorCompoentFrom(colorString, 3, 1);
            break;
        case 6: // "#RRGGBB" || "0XRRGGBB"
            alpha = 1.f;
            red = yf_colorCompoentFrom(colorString, 0, 2);
            green = yf_colorCompoentFrom(colorString, 2, 2);
            blue = yf_colorCompoentFrom(colorString, 4, 2);
            break;
        case 8: // "#AARRGGBB" || "0XAARRGGBB"
            alpha = yf_colorCompoentFrom(colorString, 0, 2);
            red = yf_colorCompoentFrom(colorString, 2, 2);
            green = yf_colorCompoentFrom(colorString, 4, 2);
            blue = yf_colorCompoentFrom(colorString, 6, 2);
            break;
            
        default:
            alpha = 0;
            red = 0;
            green = 0;
            blue = 0;
            return nil;
    }
    if (@available(iOS 10.0, *)) {
        return [UIColor colorWithDisplayP3Red:red green:green blue:blue alpha:alpha];
    } else {
        return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    }
}

/**
 返回带透度的Color，不影响透明度

 @param alpha 透度
 @return Color
 */
- (UIColor *)withAlphaComponent:(CGFloat)alpha {
    return [self colorWithAlphaComponent:alpha];
}

#pragma mark - get 
/**
 以 1 像素点生成 UIImage

 @return 生成的 Image
 */
- (UIImage *)image {
    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@end
